package com.tsingteng.cosfun.ui.message.comment.videocomment;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.MessageVideoCommentBean;
import com.tsingteng.cosfun.callback.OnPlayDetailClickListener;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.StringUtils;
import com.tsingteng.cosfun.utils.TimeUtils;

import java.util.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @我。赞我。评论我页面点击展示视频评论页适配器
 * 常胜
 * 2018.8.23
 * */
public class VideoCommentAdapter extends BaseQuickAdapter<MessageVideoCommentBean.DataBean.CommentListBean,BaseViewHolder> {
    private CircleImageView iv_header;
    private TextView tv_name_up;
    private TextView tv_comment_up;
    private TextView tv_author;
    private TextView tv_name_down;
    private TextView tv_comment_dowm;
    private TextView tv_comment_time;
    private ImageView iv_great;
    private TextView tv_great_num;
    private TextView tv_operator;
    public VideoCommentAdapter() {
        super(R.layout.item_user_comment_adapter);
    }
    private OnPlayDetailClickListener onAdapterFunctionListener;

    @Override
    protected void convert(BaseViewHolder helper, MessageVideoCommentBean.DataBean.CommentListBean item) {
        iv_header = helper.getView(R.id.iv_header);
        tv_name_up = helper.getView(R.id.tv_name_up);
        tv_comment_up = helper.getView(R.id.tv_comment_up);
        tv_author = helper.getView(R.id.tv_author);
        tv_name_down = helper.getView(R.id.tv_name_down);
        tv_comment_dowm = helper.getView(R.id.tv_comment_dowm);
        tv_comment_time = helper.getView(R.id.tv_comment_time);
        iv_great = helper.getView(R.id.iv_great);
        tv_great_num = helper.getView(R.id.tv_great_num);
        tv_operator = helper.getView(R.id.tv_operator);
        LinearLayout ll_great = helper.getView(R.id.ll_great);
        tv_operator.setVisibility(View.GONE);
        ImageUtils.LoadImage(mContext, item.getCommentUserShot()+ Config.YA_SUO_IMAGE_URL, iv_header);
        tv_name_up.setText(item.getCommentUserName());
        String content = StringUtils.matchReplaceTxt(item.getCommentText());
        SpannableString msp = StringUtils.getWeiBoContents(content, tv_comment_up,onAdapterFunctionListener);
        tv_comment_up.setText(msp);
        Long creatime = Long.valueOf(item.getCommentTime())*1000;
        tv_comment_time.setText(TimeUtils.formatChatTime(new Date(creatime)));
        if (!TextUtils.isEmpty(item.getParentCommentText())) {
            tv_comment_dowm.setText(item.getParentCommentText());
            tv_comment_dowm.setVisibility(View.VISIBLE);
        }else {
            tv_comment_dowm.setVisibility(View.GONE);
        }
        
        if (!TextUtils.isEmpty(item.getParentCommentUserName())) {
            tv_name_down.setText(item.getParentCommentUserName());
            tv_name_down.setVisibility(View.VISIBLE);
        }else {
            tv_name_down.setVisibility(View.GONE);
        }
        iv_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("tag", 3);
                bundle.putInt("user_id", item.getCommentUserId());
                bundle.putInt("workId",item.getCommentId());
                bundle.putInt(Constant.TAG, 1);
                TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
            }
        });

        ll_great.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAdapterFunctionListener !=null) {
                    onAdapterFunctionListener.onPraiseClick(item,iv_great);
                }
            }
        });
        if (item.getIsLike() == 0) {
            iv_great.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_small_icon));
        }else {
            iv_great.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_pink_small_icon));
        }
        setText(tv_great_num,item.getLikeTimes()+"");
    }

    public void setText(TextView textView, String text) {
        if (textView == null || TextUtils.isEmpty(text)) return;
        textView.setText(text);
    }


    public void setOnPlayDetailClickListener(OnPlayDetailClickListener onPlayDetailClickListener) {
        this.onAdapterFunctionListener = onPlayDetailClickListener;
    }
}
